package com.chainyoung.component_tmmarket.ui;

import android.content.Intent;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;

import com.chainyoung.common.base.BaseActivity;
import com.chainyoung.common.di.AppComponent;
import com.chainyoung.component_tmmarket.R;
import com.chainyoung.component_tmmarket.R2;
import com.chainyoung.router.RouterConstants;
import com.chainyoung.router.RouterUtils;

import butterknife.BindView;
import butterknife.OnClick;

import static com.chainyoung.common.constant.CommonStringConstants.URL;

public class SearchAppActivity extends BaseActivity {

    @BindView(R2.id.et_search)
    EditText etSearch;


    @Override
    protected void setupActivityComponent(AppComponent appComponent) {

    }

    @Override
    protected void initUI() {
        etSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    String url = etSearch.getText().toString().trim();
                    if (!url.isEmpty()) {
                        if (!url.startsWith("http")) {
                            RouterUtils.getInstance()
                                    .build(RouterConstants.PATH_OTHERAPP)
                                    .withString("title", url)
                                    .withString(URL, "http://" + etSearch.getText().toString())
                                    .navigation(mContext);
                        } else {
                            RouterUtils.getInstance()
                                    .build(RouterConstants.PATH_OTHERAPP)
                                    .withString("title", url)
                                    .withString(URL, etSearch.getText().toString().trim())
                                    .navigation(mContext);
                        }

                    }
                    return false;
                }
                return false;
            }
        });
    }

    @Override
    protected void initData() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.tmmarket_activity_search_app;
    }


    @OnClick(R2.id.tv_cancel)
    void cancel() {
        finish();
    }

}
